{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from utils import read_arff\n",
    "from apriori import apriori\n",
    "from fpgrowth import fpgrowth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>department1</th>\n",
       "      <th>department2</th>\n",
       "      <th>department3</th>\n",
       "      <th>department4</th>\n",
       "      <th>department5</th>\n",
       "      <th>department6</th>\n",
       "      <th>department7</th>\n",
       "      <th>department8</th>\n",
       "      <th>department9</th>\n",
       "      <th>grocery-misc</th>\n",
       "      <th>...</th>\n",
       "      <th>department208</th>\n",
       "      <th>department209</th>\n",
       "      <th>department210</th>\n",
       "      <th>department211</th>\n",
       "      <th>department212</th>\n",
       "      <th>department213</th>\n",
       "      <th>department214</th>\n",
       "      <th>department215</th>\n",
       "      <th>department216</th>\n",
       "      <th>total</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>...</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>high</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>t</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>...</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>...</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>t</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>...</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>...</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>?</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 217 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  department1 department2 department3 department4 department5 department6  \\\n",
       "0           ?           ?           ?           ?           ?           ?   \n",
       "1           t           ?           ?           ?           ?           ?   \n",
       "2           ?           ?           ?           ?           ?           ?   \n",
       "3           t           ?           ?           ?           ?           ?   \n",
       "4           ?           ?           ?           ?           ?           ?   \n",
       "\n",
       "  department7 department8 department9 grocery-misc  ... department208  \\\n",
       "0           ?           ?           ?            ?  ...             ?   \n",
       "1           ?           ?           ?            ?  ...             ?   \n",
       "2           ?           ?           ?            ?  ...             ?   \n",
       "3           ?           ?           ?            ?  ...             ?   \n",
       "4           ?           ?           ?            ?  ...             ?   \n",
       "\n",
       "  department209 department210 department211 department212 department213  \\\n",
       "0             ?             ?             ?             ?             ?   \n",
       "1             ?             ?             ?             ?             ?   \n",
       "2             ?             ?             ?             ?             ?   \n",
       "3             ?             ?             ?             ?             ?   \n",
       "4             ?             ?             ?             ?             ?   \n",
       "\n",
       "  department214 department215 department216 total  \n",
       "0             ?             ?             ?  high  \n",
       "1             ?             ?             ?   low  \n",
       "2             ?             ?             ?   low  \n",
       "3             ?             ?             ?   low  \n",
       "4             ?             ?             ?   low  \n",
       "\n",
       "[5 rows x 217 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "supermarket = read_arff('./dataset/supermarket.arff')\n",
    "supermarket.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "supermarket = supermarket.replace({'?': 0, 't': 1})\n",
    "all_user = []\n",
    "for i in supermarket.index.to_list():\n",
    "    user = []\n",
    "    for department in supermarket.columns:\n",
    "        \n",
    "        if supermarket.iloc[i][department] == 1:\n",
    "            user.append(department)\n",
    "        # print(data.iloc[i][department])\n",
    "    all_user.append(user)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: {frozenset({'fruit'}), frozenset({'baking-needs'}), frozenset({'vegetables'}), frozenset({'bread-and-cake'}), frozenset({'milk-cream'}), frozenset({'frozen-foods'}), frozenset({'party-snack-foods'}), frozenset({'biscuits'}), frozenset({'juice-sat-cord-ms'})}, 2: {frozenset({'bread-and-cake', 'milk-cream'}), frozenset({'bread-and-cake', 'fruit'})}}\n",
      "[[{'bread-and-cake'}, {'fruit'}, 0.6981981981981982], [{'bread-and-cake'}, {'milk-cream'}, 0.7018018018018019], [{'fruit'}, {'bread-and-cake'}, 0.7849426063470628], [{'milk-cream'}, {'bread-and-cake'}, 0.7951684246342293]]\n"
     ]
    }
   ],
   "source": [
    "freqItemSet, rules = apriori(all_user, 0.5, 0.5)\n",
    "print(freqItemSet)\n",
    "print(rules)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'party-snack-foods'}, {'juice-sat-cord-ms'}, {'biscuits'}, {'frozen-foods'}, {'baking-needs'}, {'milk-cream'}, {'bread-and-cake', 'milk-cream'}, {'vegetables'}, {'fruit'}, {'bread-and-cake', 'fruit'}, {'bread-and-cake'}]\n",
      "[[{'bread-and-cake'}, {'milk-cream'}, 0.7018018018018019], [{'milk-cream'}, {'bread-and-cake'}, 0.7951684246342293], [{'bread-and-cake'}, {'fruit'}, 0.6981981981981982], [{'fruit'}, {'bread-and-cake'}, 0.7849426063470628]]\n"
     ]
    }
   ],
   "source": [
    "freqItemSet, rules = fpgrowth(all_user, 0.5, 0.5)\n",
    "print(freqItemSet)\n",
    "print(rules)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.6 ('env': venv)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "e6a44bc4ae6224bc4bc7b3a89841677b0124c79fe22f3d101cb1ffa8756f30f3"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
